<?php
if( !defined( 'FORMFIELDMULTIPLEPICTURE' ) ): define( 'FORMFIELDMULTIPLEPICTURE', 1 );
if( !defined( 'FILE_ROOT' ) ) include_once( '../../_prerun' );

USE_LIB( 'formfieldpicture' );
USE_LIB( 'picture' );

class FormFieldMultiplePicture extends FormFieldPicture
	{
	public function __construct( $fieldname, $is_needed = false, $default = '' )
		{
		parent::__construct( $fieldname, $is_needed, $default );
		}

	public function _reset()
		{
		unset( $_POST[ $this->getName() ] );
		$this->m_value = $this->getDefaultValue();
		$this->m_has_uploaded = false;
		}

	public function setDefaultValue( $value )
		{
		$this->m_default_value = $this->m_value = $value;
		}

	public function moveTo()
		{
		$uploadfile = str_replace( '//', '/', $this->m_default_path . $_FILES[ $this->getName() ][ 'name' ] );
		$is_moved = move_uploaded_file( $_FILES[ $this->getName() ][ 'tmp_name' ], $uploadfile );
		$this->m_has_uploaded = $is_moved;
		return( $is_moved );
		}

	protected function _process( $processor = null, $ret = false )
		{
		$ret = $this->deleteFrom();
		if( $this->needUpload() )
			{
			$value = $_FILES[ $this->getName() ][ 'name' ];
			$name = basename( $value );
			if( $this->isNeeded() && isset( $this->m_default_value ) && ( $this->m_default_value != '' ) )
				{
				if( file_exists( $this->m_default_path . $this->m_default_value ) )
					unlink( $this->m_default_path . $this->m_default_value );
				}
			else
			if( $this->isNeeded() )
				{
				if( file_exists( $this->m_default_path . $name ) )
					unlink( $this->m_default_path . $name );
				}

			if( $this->getValue() != '' )
				{
				$nvalue = $this->getValue() . ';' . $value;
				$this->setValue($nvalue);
				}
			else
				{
				$this->setValue($value);
				}

			$this->moveTo();
			$this->triggerEvent( 'addfile', $name );
			}

		return( $ret );
		}

	public function deleteFrom()
		{
		if( Params::getActionValue( 'effacer', $this->getName() ) )
			{
			$names = explode(';',$this->getValue());
			foreach( $names as $i => $name )
				{
				$filename = $this->m_default_path . $name;
				if( file_exists( $filename ) && is_file( $filename ) && $name == Params::getActionValue( 'effacer', $this->getName() ))
					{
					unlink( $filename );
					unset( $names[$i] );
					$this->setValue( implode(';',$names) );
					break;
					}
				}
			$this->m_has_uploaded = false;
			$this->triggerEvent( 'removefiles', $this->getValue() );
			return( true );
			}
		return( false );
		}

	public function _clean()
		{
		$names = explode(';',$this->getValue());
		foreach( $names as $name )
			{
			$filename = $this->m_default_path . $name;
			if( file_exists( $filename ) && is_file( $filename ) )
				unlink( $filename );
			}
		$this->triggerEvent( 'removefiles', $names );
		}

	protected function _getValue()
		{
		return $this->m_value;
		}

	/**
	 * parse le template pour remplacer certaines variables
	 * @param Template $tpl # le template
	 * @param string $formname # le nom du formulaire
	 */
	public function parseTemplateTag( &$tpl, $formname )
		{
		$tpl->setVarNumber( 'FILE_MAX_SIZEINT', $this->m_maxfilesize );
		$tpl->setVar( 'FILE_MAX_SIZE', Template::size2string( $this->m_maxfilesize ) );
		$tpl->setVar( 'DEFAULT_PATH', $this->m_default_path );
		if($this->getValue()!='')
			{
			$tpl->setCondition('FILES_EXIST',true);
			$files = explode(';',trim($this->getValue()));
			$tpl->startLoop('FILES');
			foreach($files as $filename)
				{
				$tpl->newLoop('FILES');
				$tpl->setCondition('FILE_EXIST',true);
				$tpl->setVarFile( 'FILE', $filename, $this->m_default_path );
				if( is_file( $this->m_default_path . $filename ) )
					{
					$pict = new Picture( $this->m_default_path . $filename );
					$tpl->setVar( 'PICTURE_WIDTH', $pict->getWidth() );
					$tpl->setVar( 'PICTURE_HEIGHT', $pict->getHeight() );
					}
				}
			$tpl->endLoop('FILES');
			}
		$tpl->setCondition('FILES_EXIST',false);
		$tpl->setVar( 'PICTURE_WIDTH', 0 );
		$tpl->setVar( 'PICTURE_HEIGHT', 0 );
		if( $this->m_must_redim )
			{
			$tpl->setCondition( 'DIMENSIONTO', true );
			$tpl->setVar( 'DIMENSION_WIDTH', $this->m_max_width );
			$tpl->setVar( 'DIMENSION_HEIGHT', $this->m_max_height );
			$tpl->setCondition( 'CROPTO', false );
			}
		else
		if( $this->m_must_crop )
			{
			$tpl->setCondition( 'CROPTO', true );
			$tpl->setVar( 'CROP_WIDTH', $this->m_max_width );
			$tpl->setVar( 'CROP_HEIGHT', $this->m_max_height );
			$tpl->setCondition( 'DIMENSIONTO', false );
			}
		$tpl->setCondition( 'CROPTO', false );
		$tpl->setCondition( 'DIMENSIONTO', false );
		parent::parseTemplate( $tpl, $formname );
		}
	}
endif;
?>